﻿using System;
using System.Data.SqlClient;
using NUnit.Framework;
using PropertyExpression.ControllableQuery.Test.Queries;

namespace PropertyExpression.ControllableQuery.Test
{
    [TestFixture]
    public class MainTest
    {
        [Test]
        public void Test()
        {
            ExecutePartLinkQuery(
                sqlCommand => new PartLinkQuery(
                                  new WithoutInnerPartLinkSwitcher(),
                                  new PartLinkQuery.Params(
                                      Guid.NewGuid(),
                                      Guid.NewGuid(),
                                      Guid.NewGuid()
                                      ).AsParams(sqlCommand)
                                  )
                );
            ExecutePartLinkQuery(
                sqlCommand => new PartLinkQuery(
                                  new WithInnerPartLinkSwitcher(
                                      true,
                                      new WithInnerPartLinkSwitcher.Params(
                                          "en",
                                          Guid.NewGuid()
                                          ).AsParams(sqlCommand)
                                      ),
                                  new PartLinkQuery.Params(
                                      Guid.NewGuid(),
                                      Guid.NewGuid(),
                                      Guid.NewGuid()
                                      ).AsParams(sqlCommand)
                                  )
                );
            ExecutePartLinkQuery(
                sqlCommand => new PartLinkQuery(
                                  new WithInnerPartLinkSwitcher(
                                      false,
                                      new WithInnerPartLinkSwitcher.Params(
                                          "en",
                                          Guid.NewGuid()
                                          ).AsParams(sqlCommand)
                                      ),
                                  new PartLinkQuery.Params(
                                      Guid.NewGuid(),
                                      Guid.NewGuid(),
                                      Guid.NewGuid()
                                      ).AsParams(sqlCommand)
                                  )
                );
        }

        private static void ExecutePartLinkQuery(Func<SqlCommand, PartLinkQuery> queryFunc)
        {
            using (var connection = new SqlConnection(
                @"Data Source=(local)\MSSQLSERVER2008;Initial Catalog=ControllableQuery;Integrated Security=True"))
            {
                connection.Open();

                using (var sqlCommand = new SqlCommand())
                {
                    sqlCommand.Connection = connection;

                    var query = queryFunc(sqlCommand);

                    sqlCommand.CommandText = query.TransformText();

                    using (var sqlDataReader = sqlCommand.ExecuteReader())
                    {
                        //Console.WriteLine(query.GetReader(sqlDataReader).PartNumber());
                    }
                }
            }
        }
    }
}